/**
 * Copyright &copy; 2016-2022 liuhangjun All rights reserved.
 */
package com.channelsharing.hongqu.portal.api.controller.cart;

import javax.annotation.Resource;

import com.channelsharing.hongqu.portal.api.controller.BaseController;
import com.channelsharing.hongqu.portal.api.entity.*;
import com.channelsharing.hongqu.portal.api.enums.UserCouponStatus;
import com.channelsharing.hongqu.portal.api.service.CartInfoService;
import com.channelsharing.hongqu.portal.api.service.CouponService;
import com.channelsharing.hongqu.portal.api.service.UserCouponService;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.channelsharing.common.entity.Paging;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * 购物车Controller
 *
 * @author liuhangjun
 * @version 2018-06-22
 */
@Api(tags = "购物车操作接口")
@Validated
@RestController
@RequestMapping("/v1/cartInfo")
public class CartInfoController extends BaseController {

    @Resource
    private CartInfoService cartInfoService;
    @Resource
    private UserCouponService userCouponService;
    @Resource
    private CouponService couponService;

    @ApiOperation(value = "获取单条购物车")
    //@GetMapping("/{id}")
    public CartInfo findOne(@PathVariable Long id) {

        CartInfo cartInfo = cartInfoService.findOne(id);
        if (cartInfo == null)
            cartInfo = new CartInfo();

        return cartInfo;
    }

    @ApiOperation(value = "获取购物车列表")
    @GetMapping
    public Paging<CartInfo> findPaging(
            @RequestParam(defaultValue = "0") Integer offset,
            @RequestParam(defaultValue = "10") Integer limit) {

        CartInfo entity = new CartInfo();
        entity.setOffset(offset);
        entity.setLimit(limit);
        entity.setUserId(super.currentUserId());

        // 计算商品优惠后的价格
        Paging<CartInfo> cartInfoPaging = cartInfoService.findPaging(entity);
        List<CartInfo> goodsInfoList = cartInfoPaging.getRows();
        List<CartInfo> goodsInfoListNew = new ArrayList<>();
        for (CartInfo cartInfo : goodsInfoList) {

            Long goodsId = cartInfo.getGoodsId();
            Long userId = super.currentUserId();
            List<Coupon> coupons = couponService.findUsableListByUser(goodsId, userId);
            cartInfo.setCouponList(coupons);
            goodsInfoListNew.add(cartInfo);
        }

        cartInfoPaging.setRows(goodsInfoListNew);
        return cartInfoPaging;

    }

    @ApiOperation(value = "提交一条购物车")
    @PostMapping
    public void add(@RequestBody @Validated CartInfoAddRequestEntity cartInfoAddRequestEntity) {
        CartInfo entity = new CartInfo();
        BeanUtils.copyProperties(cartInfoAddRequestEntity, entity);
        entity.setUserId(super.currentUserId());

        cartInfoService.add(entity);
    }

    @ApiOperation(value = "修改一条购物车")
    @PutMapping
    public void modify(@RequestBody @Validated CartInfoModifyRequestEntity cartInfoModifyRequestEntity) {
        CartInfo entity = new CartInfo();
        BeanUtils.copyProperties(cartInfoModifyRequestEntity, entity);
        entity.setUserId(super.currentUserId());

        cartInfoService.modify(entity);
    }

    @ApiOperation(value = "删除一条购物车")
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) {
        CartInfo entity = new CartInfo();
        entity.setId(id);
        entity.setUserId(super.currentUserId());
        cartInfoService.delete(entity);
    }

    @ApiOperation(value = "批量删除购物车")
    @DeleteMapping("/batch")
    public void batchDelete(@RequestParam Long[] ids) {

        cartInfoService.batchDelete(ids, super.currentUserId());
    }

}
